﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CMDHandle.cs" company="ICodeFrog">
//   Max B.
// </copyright>
// <summary>
//   Handles execution of commands on the Windows command line.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FFMPEG_MKV2MP4
{
    using System.Diagnostics;

    /// <summary>
    /// The cmd handle.
    /// </summary>
    internal class CMDHandle
    {
        #region Fields

        /// <summary>
        /// The process.
        /// </summary>
        private readonly Process process;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CMDHandle"/> class.
        /// </summary>
        public CMDHandle()
        {
            this.process = new Process();

            this.process.StartInfo.UseShellExecute = false;
            this.process.StartInfo.RedirectStandardOutput = false;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// Executes a windows shell command.
        /// </summary>
        /// <param name="filename">
        /// The filename of the executable to run.
        /// </param>
        /// <param name="arguments">
        /// The arguments.
        /// </param>
        /// <returns>
        /// The output if any.
        /// </returns>
        public string ExecuteCommand(string filename, string arguments)
        {
            this.process.StartInfo.FileName = filename;
            this.process.StartInfo.Arguments = arguments;
            this.process.StartInfo.RedirectStandardOutput = true;
            this.process.Start();

            string output = this.process.StandardOutput.ReadToEnd();
            this.process.WaitForExit();

            return output;
        }

        #endregion
    }
}